const express = require("express")
const router = express.Router()
const mattersDB = require("../../db/matters")
const recordDB = require("../../db/record")

// 公益事项  ---- 查询
router.get("/matters/find", async (req, res) => {
  try {
    const { isAll } = req.query
    // 查询所有
    if (isAll === "true") {
      const mattersList = await mattersDB.find()
      res.send({
        code: 0,
        mes: "公益事项全部活动",
        data: {
          mattersList,
        },
      })
    }
    // 只查询选中展示的
    else {
      const mattersList = await mattersDB.find(
        { isShow: true },
        {},
        { sort: { time: -1 } }
      )
      res.send({
        code: 0,
        mes: "公益事项活动列表",
        data: {
          mattersList,
        },
      })
    }
  } catch (error) {
    res.send({
      code: 5,
      mes: "服务器异常，前稍后再试",
    })
  }
})

// 公益项目数据 --- 查询
router.get("/record/find", async (req, res) => {
  try {
    const { isAll } = req.query
    // 查询所有
    if (isAll === "true") {
      const recordList = await recordDB.find()
      res.send({
        code: 0,
        mes: "公益项目所有数据",
        data: {
          recordList,
        },
      })
    }
    // 只查询选中展示的
    else {
      const recordList = await recordDB.find(
        { isShow: true },
        {},
        { sort: { time: 1 } }
      )
      res.send({
        code: 0,
        mes: "公益项目数据",
        data: {
          recordList,
        },
      })
    }
  } catch (error) {
    res.send({
      code: 5,
      mes: "服务器异常，前稍后再试",
    })
  }
})

// 身份鉴权

// 需要身份进行的操作
router.use("/", require("./power"))

module.exports = router
